<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21
 * Time: 14:27
 */

namespace app\api\controller;

use JPush\Client as JPush;
use think\Db;
use think\Controller;

class Jpushmember extends Controller
{
//appkey
    static public function app_key(){

        $app_key = "c3a7dcf600affbd44b26edf8";//极光官网分配的
        return $app_key;
    }

    //master_secret,用于服务器端API调用时与Appkey 配合使用达到鉴权的目的
    static public function master_secret(){

        $master_secret = "4a11fd736423728ad039ef89";//极光官网分配的
        return $master_secret;
    }

    //将json格式转换成数组格式
    function json_array($result){
        $result_json = json_encode($result);
        return json_decode($result_json,true);
    }

    //获取alias和tags
    public function getDevices($registrationID){

        $app_key = $this->app_key();
        $master_secret = $this->master_secret();

        $client = new JPush($app_key, $master_secret);

        $result = $client->device()->getDevices($registrationID);

        return $result;

    }

    //添加tags
    public function addTags($registrationID,$tags){

        $app_key = $this->app_key();
        $master_secret = $this->master_secret();

        $client = new JPush($app_key, $master_secret);

        $result = $client->device()->addTags($registrationID,$tags);

        return $result;

    }

    //移除tags
    public function removeTags($registrationID,$tags){

        $app_key = $this->app_key();
        $master_secret = $this->master_secret();

        $client = new JPush($app_key, $master_secret);

        $result = $client->device()->removeTags($registrationID,$tags);

        return $result;

    }

    //标签推送
    public function pushTag($tag,$content){

        $app_key = $this->app_key();
        $master_secret = $this->master_secret();

        $client = new JPush($app_key, $master_secret);

        $tags = implode(",",$tag);

        $client->push()
            ->setPlatform(array('ios', 'android'))
            ->addTag($tags)                          //标签
            ->setNotificationAlert($content)           //内容
            ->send();

        return $this->json_array($client);

    }

    /**
     * 别名推送
     *@param $userids
     *@param $content
     *@return mixed
     * */
    public function pushAlias($userids,$content){

        $app_key = $this->app_key();
        $master_secret = $this->master_secret();

        $client = new JPush($app_key, $master_secret);

        $alias = [];

        foreach ($userids as $k => $v){
            $mobile = Db::name('member')->where(['id' => $v])->value('username');
            array_push($alias,$mobile);
        }

        $result = $client->push()
            ->setPlatform(array('ios', 'android'))
            ->addAlias($alias)                      //别名
            ->setNotificationAlert($content)        //内容
            ->send();

        return $this->json_array($result);

    }

    //向所有设备推送消息（用于开发阶段的测试）
    function sendNotifyAll($message){
        $message = "测试推送所有设备";
        $app_key = $this->app_key();
        $master_secret = $this->master_secret();
        $client = new JPush($app_key, $master_secret);

        $result = $client->push()
            ->setPlatform('all')
            ->addAllAudience()                      //别名
            ->setNotificationAlert($message)        //内容
            ->send();

        return $result;
    }


}